package com.time.admin.domain.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;

/**
 * yml 转换工具类
 *
 * @author: HuangQi
 * @date: Created in 14:54 2025/11/12
 * @description: bean 类和 yml 转换工具类
 */
public class YamlSerializerUtil {

    private static final ObjectMapper yamlMapper;

    static {
        YAMLFactory factory = new YAMLFactory()
                .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
        yamlMapper = new ObjectMapper(factory);
        yamlMapper.findAndRegisterModules();
        // 可选：忽略 null 值
        yamlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    public static String toYaml(Object bean) throws Exception {
        if (bean == null) {
            throw new IllegalArgumentException("Bean to serialize must not be null");
        }
        return yamlMapper.writeValueAsString(bean);
    }
}

